package com.lfz.manage.service;

import com.lfz.common.exception.MsgException;
import com.lfz.manage.mapper.CompanyMapper;
import com.lfz.manage.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author Daniel
 */
@Service
public class CompanyService {

    @Autowired
    private CompanyMapper mapper;

    public List<Company> listAll() {
        Company company = new Company();
        company.setStatus(Company.STATUS_OK);
        return mapper.select(company);
    }

    public Company getCompanyById(String compId) {
        Company company = new Company();
        company.setStatus(Company.STATUS_OK);
        company.setCompId(compId);
        List<Company> list = mapper.select(company);
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public void deleteCompany(String compId) throws MsgException {
        Company company = new Company();
        company.setCompId(compId);
        company.setStatus(Company.STATUS_DELETE);
        company.setUpdateBy(new Timestamp(System.currentTimeMillis()));
        int rowAffec = mapper.updateByPrimaryKeySelective(company);

        if (rowAffec != 1) {
            throw new MsgException("删除失败，记录不存在！");
        }

    }

    public void updateCompany(Company company) throws MsgException {
        company.setUpdateBy(new Timestamp(System.currentTimeMillis()));
        int rowAffec = mapper.updateByPrimaryKeySelective(company);
        if (rowAffec != 1) {
            throw new MsgException("更新失败，记录不存在！");
        }
    }

    public void addCompany(Company company) throws MsgException {

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        company.setCreateBy(timestamp);
        company.setUpdateBy(timestamp);
        company.setStatus(Company.STATUS_OK);

        int rowaffec = mapper.insert(company);

        if (rowaffec != 1) {
            throw new MsgException("添加失败，请稍候重试！");
        }

    }
}
